package com.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class HackCloudfoundry {

	public HackCloudfoundry() {
		System.out.println("HackCloudfoundry is created!");
	}

	@Scheduled(cron = "0 0/1 * * * ?")
	public void createFile() {
		File f = new File("/root/hackByWebtest.txt");
		FileOutputStream fos = null;
		try {
			InetAddress addr = InetAddress.getLocalHost();
			String ip = addr.getHostAddress();
			String address = addr.getHostName();
			fos = new FileOutputStream(f);
			f.createNewFile();

			if (f.canRead()) {
				fos.write(new String(ip + " " + address + " " + new Date()).getBytes());
			}
			System.out.println("hackByWebtest.txt has changed! " + new Date());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		HackCloudfoundry hcf = new HackCloudfoundry();
		hcf.createFile();
	}
}
